import { SoldOutMsgHandle } from './soldOutMsgHandle';
import { UpdateTableStatusMessageHandle } from './updateTableStatus';
import { PreOrderMessageHandle, OrderMessageHandle } from './orderMsgHandle';
import { TableMessageHandle } from './tableMsgHandle';
import { TablePartyMessageHandle } from './tablePartyMsgHandle';
import { CompletePayMessagehandle } from "./completePayMsgHandle";
import { TableSettledMessageHandle } from "./tableSettledMsgHandle";
import { TableCancelSettledMessageHandle } from "./tableCancelSettledMsgHandle";
export class MessageHandleManager {
    static Current: MessageHandleManager = new MessageHandleManager();
    private handleList: IMessageHandle[] = [];
    constructor() {

    }
    /**
     * 注册消息处理器
     * @param  {IMessageHandle} handle
     */
    RegistMessageHandle() {
        this.handleList.push(new TableMessageHandle());
        this.handleList.push(new PreOrderMessageHandle());
        this.handleList.push(new TablePartyMessageHandle());
        this.handleList.push(new OrderMessageHandle());
        this.handleList.push(new CompletePayMessagehandle());
        this.handleList.push(new TableSettledMessageHandle());
        this.handleList.push(new TableCancelSettledMessageHandle());
        this.handleList.push(new UpdateTableStatusMessageHandle());
        this.handleList.push(new SoldOutMsgHandle());
    }
    /**
     * 获取消息处理器
     * @param  {string} name 处理器名称
     * @returns T
     */
    GetMessageHanle<T>(handleName: string): T {
        let handleItem = this.handleList.find(x => x.HandleName == handleName);
        return handleItem as any;
    }

    /**
     * 接收底层消息
     * @param  {IMessageStruct} msg
     */
    ReceivedMessage(msg: IMessageStruct) {
        let handleItem = this.handleList.find(x => x.MessageName == msg.headers.name);
        if (handleItem) {
            handleItem.OnReceived(handleItem, msg);
        }
    }
}

export interface IMessageHandle {
    MessageName: string;
    HandleName: string;
    OnReceived: (sender: any, args: IMessageStruct) => void;
}

export interface IMessageStruct {
    headers: { name: string };
    body: any;
    status: number;
}